package com.lmt.api;

import com.lmt.base.ResultData;
import com.lmt.entity.DigitalDictionary;
import com.lmt.entity.search.DigitalDictionarySearch;
import com.lmt.service.DigitalDictionaryService;
import com.lmt.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@RestController
@RequestMapping("/DigitalDictionaryApi")
public class DigitalDictionaryApi {

	private final DigitalDictionaryService digitalDictionaryService;

	@Autowired
	public DigitalDictionaryApi(DigitalDictionaryService digitalDictionaryService) {
		this.digitalDictionaryService = digitalDictionaryService;
	}

	/**
	 *
	 * @apiNote 保存数据字典
	 * @param digitalDictionary 保存的数据字典
	 * @return ResultData
	 */
	@RequestMapping("/save")
	public ResultData save(@RequestBody DigitalDictionary digitalDictionary) {
		return this.digitalDictionaryService.save(digitalDictionary);
	}

	@RequestMapping("/findOneById")
	public ResultData findOneById(@RequestBody DigitalDictionarySearch digitalDictionarySearch) {
		String id = digitalDictionarySearch.getId();
		if (null == id) {
			return ResultUtil.newFail("传入的数据字典ID为空");
		}
		return this.digitalDictionaryService.findOneById(id);
	}

	@RequestMapping("/update")
	public ResultData update(@RequestBody DigitalDictionary digitalDictionary) {
		String id = digitalDictionary.getId();
		if (null == id) {
			return ResultUtil.newFail("传入的数据字典ID为空");
		}
		return this.digitalDictionaryService.update(digitalDictionary);
	}

	@RequestMapping("/search")
	public ResultData search(@RequestBody DigitalDictionarySearch digitalDictionarySearch) {
		return this.digitalDictionaryService.search(digitalDictionarySearch);
	}

	@RequestMapping("/getChildrenForSelect")
	public ResultData getChildrenForSelect(@RequestBody DigitalDictionarySearch digitalDictionarySearch) {
		return this.digitalDictionaryService.getChildrenForSelect(digitalDictionarySearch.getParentCode());
	}

	@RequestMapping("/getParentCodeTree")
	public ResultData findChildrenForTree() {
		return this.digitalDictionaryService.getParentCodeTree();
	}

	@RequestMapping("/delete")
	public ResultData delete(@RequestBody List<DigitalDictionary> digitalDictionaries) {
		return this.digitalDictionaryService.delete(digitalDictionaries);
	}

}
